﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public static class PatientDataAccess
    {
        public static bool InsertNewPatient(Patient newPatient)
        {
            if (newPatient != null)
            {
                string stringConnection = "insert into Patient (Ci,FirstName,LastName,Gender, BirthDay, Phone, CellPhone, Address, Email,Active, Picture) values('" + newPatient.CIPatient + "','" + newPatient.FirstName + "','" + newPatient.LastName + "','" + newPatient.Gender + "'," + newPatient.Birthday + "," + newPatient.Phone + ",'" + newPatient.CellPhone + "','" + newPatient.Address + "','" + newPatient.Email + "',1,'" + newPatient.ImagePatient + "') ";
                return ExecuteConsult(stringConnection);
            }
            else
            {
                return false;
            }
        }

        public static DataTable LoadDataGridViewPatient()
        {
            string stringConnection = "select * from Patient";
            using (DBConnection connection = new DBConnection())
            {
                return connection.ConsultationSelectDataTable(stringConnection);
            }
        }
        public static bool DisablePatient(string idPatient)
        {
            string stringConnection = "update Patient set StatePatient=0 where CiPatient='" + idPatient + "'";
            return ExecuteConsult(stringConnection);
        }

        private static bool ExecuteConsult(string stringConnection)
        {
            using (DBConnection connection = new DBConnection())
            {
                if (connection.CommandExecuteConsult(stringConnection))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public static bool ModifyPatient(string idPatient, Patient patient)
        {
            if (patient != null)
            {
                string stringConnection = "update Patient set CiPatient= '" + patient.CIPatient + "', FirstName='" + patient.FirstName + "', LastName='" + patient.LastName + "',Gender='" + patient.Gender + "',BirthDay= '" + patient.Birthday + "',Phone=" + patient.Phone + ",CellPhone=" + patient.CellPhone + ",Address= '" + patient.Address + "',Email='" + patient.Email + "' where CiPatient='" + idPatient + "'";
                return ExecuteConsult(stringConnection);
            }
            else
            {
                return false;
            }
        }


        public static DataTable LoadDataGridViewSearchPatient(string searchPatient)
        {
            string stringConnection = "Select * From Patient where CiPatient like'%" + searchPatient + "%'";
            using (DBConnection connection = new DBConnection())
            {
                if (connection.ConsultationSelect(stringConnection))
                {
                    return connection.ConsultationSelectDataTable(stringConnection);
                }
                else
                {
                    stringConnection = "Select * From Patient where FirstName like'%" + searchPatient + "%'";
                    if (connection.ConsultationSelect(stringConnection))
                    {
                        return connection.ConsultationSelectDataTable(stringConnection);
                    }
                    else
                    {
                        stringConnection = "Select * From Patient where LastName like'%" + searchPatient + "%'";
                        if (connection.ConsultationSelect(stringConnection))
                        {
                            return connection.ConsultationSelectDataTable(stringConnection);
                        }
                        else
                        {
                            return null;
                        }

                    }
                }
            }
        }

        public static DataSet LoadDataPatient(string searchPatient)
        {
            string stringConnection = "select FirstName, LastName from Patient where CiPatient='"+searchPatient+"'";
            using (DBConnection connection = new DBConnection())
            {
                if (connection.ConsultationSelect(stringConnection))
                {
                    return connection.ExtractData(stringConnection);
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
